package com.eqdd.yiqidian.ui.gongneng.renshi.kaoqin.setting;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.AddBanciActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.FourBean;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.jakewharton.rxbinding.view.RxView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class AddBanciActivity extends BaseActivity {
    private AddBanciActivityCustom dataBinding;
    private BaseQuickAdapter<FourBean, BaseViewHolder> quickAdapterNext;
    private List<FourBean> fourBeans;
    private int clickNexted;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_banci);
        initTopTitleBar(View.VISIBLE, "添加班次");
        initTopRightText("提交", v -> {

        });
        RxView.clicks(dataBinding.fabAdd)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe((v) -> {
                    startActivityForResult(AddTimeActivity.class, null, Config.ADD_TIME);
                });
        RxView.clicks(dataBinding.rlName)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe((v) -> {
                    Bundle bundle = new Bundle();
                    bundle.putString("content", dataBinding.tvVerticalContent.getText().toString());
                    bundle.putString("tip", "");
                    bundle.putString("title", "班次名称");
                    startActivityForResult(EditTextActivity.class, bundle, Config.EDIT_TEXT);
                });

    }

    @Override
    public void initData() {

        dataBinding.tvVerticalTitle.setText("班次名称");
        dataBinding.tvVerticalContent.setHint("请输入");

    }

    @Override
    public void setView() {
        if (quickAdapterNext == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
            quickAdapterNext = new BaseQuickAdapter<FourBean, BaseViewHolder>(R.layout.list_item_12, new ArrayList<>()) {

                @Override
                protected void convert(BaseViewHolder helper, FourBean item) {

                    helper.setText(R.id.tv_upper_left_content, "上班时间"+item.getFirst())
                            .setText(R.id.tv_down_left_content, item.getSecond())
                            .setText(R.id.tv_upper_right_content, "下班时间"+item.getThird())
                            .setText(R.id.tv_down_right_content, item.getFour());
                }
            };
            dataBinding.recyclerView.setAdapter(quickAdapterNext);
            if (fourBeans != null) {
                ArrayList<FourBean> temp = new ArrayList<>();
                temp.addAll(fourBeans);
                quickAdapterNext.notifyDataChanged(temp, true);

            }
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((rv, position, v) -> {
                        clickNexted = position;
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(Config.FOUR_BEAN, quickAdapterNext.getItem(position));
                        startActivityForResult(AddTimeActivity.class, bundle, Config.ADD_TIME);
                    });
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.ADD_TIME) {
            if (resultCode==Config.SUCCESS||resultCode==Config.CHANGE) {
                String startTime = data.getStringExtra(Config.START_TIME);
                String startTimeRange = data.getStringExtra(Config.START_TIME_RANGE);
                String endTime = data.getStringExtra(Config.END_TIME);
                String endTimeRange = data.getStringExtra(Config.END_TIME_RANGE);
                if (resultCode == Config.SUCCESS) {
                    FourBean fourBean = new FourBean(startTime, startTimeRange, endTime, endTimeRange);
                    quickAdapterNext.addData(fourBean);
                } else if (resultCode == Config.CHANGE) {
                    quickAdapterNext.getItem(clickNexted).setFirst(startTime);
                    quickAdapterNext.getItem(clickNexted).setSecond(startTimeRange);
                    quickAdapterNext.getItem(clickNexted).setThird(endTime);
                    quickAdapterNext.getItem(clickNexted).setFour(endTimeRange);
                    quickAdapterNext.notifyItemChanged(clickNexted);
                }
            }
        }
        if (requestCode == Config.EDIT_TEXT && resultCode == Config.INTENT_CONTENT) {
            dataBinding.tvVerticalContent.setText(data.getStringExtra("content"));
        }
    }
}
